package org.csu.group4.jpetstore.controller;

//import cn.hutool.json.JSONObject;
//import com.zhenzi.sms.ZhenziSmsClient;
import org.csu.group4.jpetstore.mybatis.entity.Account;
import org.csu.group4.jpetstore.mybatis.entity.SignOn;
import org.csu.group4.jpetstore.result.Result;
import org.csu.group4.jpetstore.result.ResultFactory;
import org.csu.group4.jpetstore.service.SignOnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
public class AccountController {
    @Autowired
    SignOnService signOnService;


    @GetMapping(value = "/account/signOn")
    public ModelAndView getLoginInPage(){
        return new ModelAndView("account/SignonForm");
    }



    @ResponseBody
    @PostMapping(value = "/account/login")
    public Result login(@RequestBody SignOn signOn){
        SignOn result =  signOnService.login(signOn);
        if(result.getUsername().equals(signOn.getUsername())){
            HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                    .getRequest().getSession();
            session.setAttribute("localUser",signOn.getUsername());
            return ResultFactory.getSuccessResult("login success");
        }
        return ResultFactory.getFailResult("login denied");
    }

    /*@ResponseBody
    @PostMapping(value = "/account/otherlogin")
    public Result loginbyphone(@RequestBody String username,@RequestBody String textpass,HttpServletRequest httpServletRequest){
        HttpSession session=  httpServletRequest.getSession();
        JSONObject userCode = (JSONObject)session.getAttribute("code");
        //  System.out.println(userCode.getStr("code"));
        // System.out.println(inputCode);
        String vcode = userCode.getStr("code");
        String tcode ="[\""+textpass+"\"]";
        if(tcode.equals(vcode)){
            HttpSession session2 = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                    .getRequest().getSession();
            session2.setAttribute("localUser",);
            return ResultFactory.getSuccessResult("login success");
        }
        return ResultFactory.getFailResult("login denied");
    }*/

    /*@ResponseBody
    @GetMapping(value = "/account/telesend")
    public boolean getCode(@RequestParam("memPhone") String phoneNumber, HttpServletRequest request) {
    //短信平台相关参数
            String apiUrl = "https://sms_developer.zhenzikj.com";
            //榛子云系统上获取
            String appId = "108629";
            String appSecret = "d2b4e705-e682-477d-a194-24b247bca044";
            //System.out.println("vcode" + phoneNumber);
        try{
            JSONObject json;
            ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
            String code = String.valueOf(new Random().nextInt(899999) + 100000);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number", phoneNumber);
            params.put("templateId", "4527");
            //这个参数就是短信模板上那两个参数
            String[] templateParams = new String[2];
            templateParams[0] = code;
            templateParams[1] = "1分钟";
            params.put("templateParams", templateParams);
            String result = client.send(params);

            json = new JSONObject(result);
            if(json.getInt("code")!=0)
                return false;
            json = new JSONObject();
            json.append("memPhone",phoneNumber);
            json.append("code",code);
            json.append("createTime",System.currentTimeMillis());
            // 将认证码存入SESSION
            request.getSession().setAttribute("code",json);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }*/

    @ResponseBody
    @GetMapping(value = "/account/loginOut")
    public Result loginOut(){
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        session.setAttribute("localUser","");
        return ResultFactory.getSuccessResult("login out");
    }




    @GetMapping(value = "/account/getRegister")
    public ModelAndView getRegister(){
        return new ModelAndView("account/NewAccountForm");
    }


    @GetMapping(value = "/account/userInfo")
    public ModelAndView getInfo(){
        ModelAndView modelAndView;
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest().getSession();
        String username = (String) session.getAttribute("localUser");
        System.out.println(username);
        if(username == null || username.equals("")){
            modelAndView = new ModelAndView("account/SignonForm");
        }else{
            modelAndView = new ModelAndView("account/EditAccountForm");
            modelAndView.addObject("account",signOnService.getAccountByUsername(username));
        }
        return modelAndView;
    }

    @PostMapping(value = "/account/userInfo")
    @ResponseBody
    public Result setInfo(@RequestBody Account account){
        System.out.println(account);
        signOnService.setAccount(account);
        return ResultFactory.getSuccessResult("更改成功");
    }
}
